package com.emm.yixun.system.service.impl;
import java.util.List;

import javax.jws.WebService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.BeanUtils;

import com.emm.yixun.common.exception.ResponseCode;
import com.emm.yixun.common.page.Page;
import com.emm.yixun.common.response.Response;
import com.emm.yixun.common.response.ResponseList;
import com.emm.yixun.basic.service.BaseCodeTypeService;
import com.emm.yixun.basic.service.BaseCodeValueService;
import com.emm.yixun.common.model.BaseCodeType;
import com.emm.yixun.common.model.BaseCodeValue;
import com.emm.yixun.common.model.User;
import com.emm.yixun.common.model.dto.BaseCodeTypeDto;
import com.emm.yixun.common.model.dto.BaseCodeValueDto;
import com.emm.yixun.common.model.enums.DelFlagEnum;
import com.emm.yixun.system.service.BaseCodeTypeServiceFacade;

@Service("baseCodeTypeServiceFacade")
@WebService(name="com.emm.yixun.system.service.BaseCodeTypeServiceFacade")
public class BaseCodeTypeServiceFacadeImpl implements BaseCodeTypeServiceFacade{
	private  final Logger   logger = LoggerFactory.getLogger(this.getClass()); 
	
	@Autowired
	private BaseCodeTypeService service;
	
	@Autowired
	private BaseCodeValueService baseCodeValueService;

	@Override
	public Response<BaseCodeTypeDto> save(BaseCodeTypeDto dto) {
		Response<BaseCodeTypeDto> response=new Response<BaseCodeTypeDto>();
		try {
			if(null==dto){
				response.fail(ResponseCode.PARAM_INVALID.getCode(), ResponseCode.PARAM_INVALID.formatMessage("保存"));
				return response;
			}
			BaseCodeTypeDto mDto=service.create(dto);
			if(null==mDto){
				response.fail("80000", "保存数据错误");
				return response;
			}
			response.success(mDto);
		} catch (Exception e) {
			response.fail(ResponseCode.SYSTEM_EXCEPTION.getCode(), ResponseCode.SYSTEM_EXCEPTION.getMessageTemplate());
			logger.error("保存数据错误：{}",e);
		}
		return response;
	}

	@Override
	public Response<Integer> updateByPrimaryKey(BaseCodeTypeDto dto,Boolean selective) {
		Response<Integer> response=new Response<Integer>();
		try {
			if(null==selective || null==dto || null==dto.getId()){
				response.fail(ResponseCode.PARAM_INVALID.getCode(), ResponseCode.PARAM_INVALID.formatMessage("更新"));
				return response;
			}
			
			if(selective){
				int count=service.update(dto);
				if(count>0){
					response.success(Integer.valueOf(count));
				}else{
					response.fail("90001", "更新数据失败");
				}
			}else{
				response.fail("90000", "暂未实现,修改参数selective=true即可");
			}
		} catch (Exception e) {
			response.fail(ResponseCode.SYSTEM_EXCEPTION.getCode(), ResponseCode.SYSTEM_EXCEPTION.getMessageTemplate());
			logger.error("更新数据错误：{}",e);
		}
		return response;
	}

	@Override
	public Response<Integer> deleteByPrimaryKey(Long id) {
		Response<Integer> response=new Response<Integer>();
		try {
			if(null==id){
				response.fail(ResponseCode.PARAM_INVALID.getCode(), ResponseCode.PARAM_INVALID.formatMessage("删除"));
				return response;
			}
			BaseCodeTypeDto mDto=new BaseCodeTypeDto();
			mDto.setId(id);
			int count=service.destroy(mDto);
			if(count>0){
				response.success(Integer.valueOf(count));
			}else{
				response.fail("90001", "删除数据失败");
			}
		} catch (Exception e) {
			response.fail(ResponseCode.SYSTEM_EXCEPTION.getCode(), ResponseCode.SYSTEM_EXCEPTION.getMessageTemplate());
			logger.error("删除数据错误：{}",e);
		}
		return response;
	}
	
    @Override
	public Response<BaseCodeType> findById(Long id) {
		Response<BaseCodeType> response=new Response<BaseCodeType>();
		try {
			if(null==id){
				response.fail(ResponseCode.PARAM_INVALID.getCode(), ResponseCode.PARAM_INVALID.formatMessage("主键查询"));
				return response;
			}
			BaseCodeType entity=service.selectById(id);
			if(null==entity){
				response.fail("90001", "主键查询数据失败");
			}else{
				response.success(entity);;
			}
		} catch (Exception e) {
			response.fail(ResponseCode.SYSTEM_EXCEPTION.getCode(), ResponseCode.SYSTEM_EXCEPTION.getMessageTemplate());
			logger.error("主键查询数据错误：{}",e);
		}
		return response;
	}

	@Override
	public ResponseList<BaseCodeType> findByDto(BaseCodeTypeDto dto) {
		ResponseList<BaseCodeType> response=new ResponseList<BaseCodeType>();
		try {
			if(null==dto){
				response.fail(ResponseCode.PARAM_INVALID.getCode(), ResponseCode.PARAM_INVALID.formatMessage("查询"));
				return response;
			}
			List<BaseCodeType> list=service.selectListByDto(dto);
			if(null==list){
				response.fail(ResponseCode.NO_DATA_FOUND.getCode(), ResponseCode.NO_DATA_FOUND.formatMessage("查询"));
				return response;
			}
			response.success(list);
		} catch (Exception e) {
			response.fail(ResponseCode.SYSTEM_EXCEPTION.getCode(), ResponseCode.SYSTEM_EXCEPTION.getMessageTemplate());
			logger.error("查询数据错误：{}",e);
		}
		return response;
	}

	@Override
	public Page<BaseCodeType> findPageByDto(BaseCodeTypeDto dto, Integer begin,Integer row) {
		Page<BaseCodeType> response=new Page<BaseCodeType>();
		try {
			if(null==dto || null==begin || null==row){
				response.setSuccess(false);
				response.setErrorMsg("参数不合法");
				return response;
			}
			Page<BaseCodeType> page = service.getPageModel(dto, begin, row);
			if(null==page.getRows()){
				response.setSuccess(false);
				response.setErrorMsg("分页数据不存在");
				return response;
			}
			response=page;
			response.setSuccess(true);
		} catch (Exception e) {
			response.setSuccess(false);
			response.setErrorMsg(ResponseCode.SYSTEM_EXCEPTION.getMessageTemplate());
			logger.error("查询分页数据错误：{}",e);
		}
		return response;
	}

	/**
	 * 复制业务参数
	 */
	@Override
	@Transactional
	public Response<String> copyBaseCode(Long projectId, Long copyPid, User user) {
		Response<String> res = new Response<String>();
		res.setSuccess(false);
		
		//删除原BaseCodeType
		BaseCodeTypeDto typeEntity = new BaseCodeTypeDto();
		typeEntity.setMerchantId(user.getMerchantId());
		typeEntity.setProjectId(projectId);
		service.destroy(typeEntity);
		
		//删除原BaseCodeValue
		BaseCodeValueDto valueEntity = new BaseCodeValueDto();
		valueEntity.setMerchantId(user.getMerchantId());
		valueEntity.setProjectId(projectId);
		baseCodeValueService.destroy(valueEntity);
		
		//获取新BaseCodeType
		BaseCodeTypeDto typeDto = new BaseCodeTypeDto();
		typeDto.setMerchantId(user.getMerchantId());
		typeDto.setProjectId(copyPid);
		typeDto.setDelFlag(DelFlagEnum.UNDEL.getNo());
		List<BaseCodeType> typeList = service.selectListByDto(typeDto);
		if (typeList == null || typeList.size() == 0) {
			res.setErrorCode("9999");
			res.setResultMessage("未查询到所选项目的业务参数");
			return res;
		}
		
		//复制BaseCodeType
		for(BaseCodeType type : typeList){
			BaseCodeType bct = new BaseCodeType();
			BeanUtils.copyProperties(type, bct);
			bct.setId(null);
			bct.setProjectId(projectId);
			bct.setCreateUserId(user.getId());
			bct = service.add(bct);
			
			//获取对应的BaseCodeValue
			BaseCodeValueDto valueDto = new BaseCodeValueDto();
			valueDto.setMerchantId(user.getMerchantId());
			valueDto.setProjectId(copyPid);
			valueDto.setTypeId(type.getId());
			typeDto.setDelFlag(DelFlagEnum.UNDEL.getNo());
			List<BaseCodeValue> valueList = baseCodeValueService.selectListByDto(valueDto);
			if (valueList == null || valueList.size() == 0) {
				continue;
			}
			//复制BaseCodeValue
			for(BaseCodeValue value : valueList){
				value.setId(null);
				value.setProjectId(projectId);
				value.setTypeId(bct.getId());
				value.setCreateUserId(user.getId());
				baseCodeValueService.add(value);
			}
		}
		res.setSuccess(true);
		
		return res;
	}
	
}
